import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  { path: '/', redirect: '/index' },
  { path: '/login', component: () => import('@/views/login/Login.vue') },
  {
    path: '/index',
    component: () => import('@/views/Index.vue'),
    redirect: '/index/main',
    children: [
      {
        path: '/index/main',
        component: () => import('@/views/main/Index.vue')
      },
      {
        path: '/basicData/Calendar',
        component: () => import('@/views/basicData/Index.vue')
      },
      // 运价维护
      {
        path: '/tariff/freightRate',
        component: () => import('@/views/rariffMaintenance/Basics/Index.vue')
      },
      {
        path: '/tariff/packageCabin',
        component: () =>
          import('@/views/rariffMaintenance/PackageCabin/Index.vue')
      },
      {
        path: '/tariff/singleTicket',
        component: () =>
          import('@/views/rariffMaintenance/SingleTicket/Index.vue')
      }
    ]
  },
  {
    path: '/:patchMatch(.*)*',
    name: 'not-found',
    component: () => import('@/views/404/index.vue')
  }
]

const router = createRouter({
  routes,
  history: createWebHashHistory()
})

router.beforeEach((to) => {
  if (to.path !== '/login') {
    const token = localStorage.getItem('token')
    if (!token) return '/login'
  }
})

export default router
